<?php

namespace api\modules\v1\controllers;

use api\controllers\RestController;
use api\modules\v1\services\CourseCatalog;
use api\modules\v1\services\CourseStudy;
use common\services\CourseContent;

/**
 * Default controller for the `v1` module
 */
class CatalogController extends RestController
{
    public $modelClass = 'api\modules\v1\services\CourseCatalog';

    /**
     * 重写create操作，进行用户注册或添加
     * @name: actions
     * @return array
     * @author: rickeryu <lhyfe1987@163.com>
     * @time: 17/11/21 下午4:57
     */
    public function actions() {
        $actions = parent::actions(); // TODO: Change the autogenerated stub
        unset($actions['create'], $actions['update'], $actions['delete'], $actions['index']);
        return $actions;
    }

    /**
     * 返回分类列表
     * @name: actionIndex
     * @return array
     * @author: rickeryu <lhyfe1987@163.com>
     * @time: 2017/11/27 下午2:34
     */
    public function actionIndex($course_id) {

        $catalog = CourseCatalog::lists(['status' => CourseCatalog::STATUS_ACTIVE, 'is_lock' => 0, 'course_id' => $course_id],'id,title,parent_id,course_id',true,'tier ASC,ordernum ASC');
        $content = CourseContent::lists(['status' => CourseContent::STATUS_ACTIVE, 'course_id' => $course_id],'id,title,course_catalog_id,course_id,file_id',true,'ordernum ASC,id ASC');

        $study = CourseStudy::lists(['status' => CourseStudy::STATUS_ACTIVE, 'course_id' => $course_id,'user_id'=>\Yii::$app->getUser()->id],'parent_course_catalog_id,course_catalog_id,course_id,course_content_id',true,'id ASC');
        $read = [
            'catalog' =>[],
            'content' => [],
        ];
        foreach ($study as $val){
            $read['catalog'][] = $val['parent_course_catalog_id'];
            $read['catalog'][] = $val['course_catalog_id'];
            $read['content'][] = $val['course_content_id'];
        }

        $contents = [];
        foreach ($content as $val) {
            if (!isset($contents[$val['course_catalog_id']])) {
                $contents[$val['course_catalog_id']] = [];
            }
            if( in_array($val['id'],$read['content']) ){
                $val['read'] = 1;
            }else{
                $val['read'] = 0;
            }
            array_push($contents[$val['course_catalog_id']], $val);
        }

        $result = [];
        foreach ($catalog as $val) {
            if( in_array($val['id'],$read['catalog']) ){
                $val['read'] = 1;
            }else{
                $val['read'] = 0;
            }
            if (!$val['parent_id']) {
                $result[$val['id']] = $val;
                $result[$val['id']]['sub'] = [];
            } else {
                if (isset($result[$val['parent_id']]['sub'])) {
                    if (isset($contents[$val['id']])) {
                        $val['content'] = $contents[$val['id']];
                    }
                    array_push($result[$val['parent_id']]['sub'], $val);
                }
            }
        }
        return array_values($result);
    }

}
